package com.dao;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import com.entity.HistoryItem;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class ModeDAO {
	public static final String MODE_FILE_NAME = "ocrmode.bin";
	private Context context;

	public ModeDAO(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
		try {
			Log.d("---OCR  mode constructer", "thoainguyen");
			int mode = 0;
			mode = readMode();
			/*
			 * mac dinh lan dau la 1
			 */
			if (mode != 0)
				saveMode(mode);
			else
				saveMode(1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void saveMode(int mode) throws IOException {
		try {
			Log.d("---OCR save mode:" + mode, "thoainguyen");
			FileOutputStream fo = context.openFileOutput(MODE_FILE_NAME,
					Context.MODE_WORLD_WRITEABLE);
			BufferedOutputStream bo = new BufferedOutputStream(fo);
			ObjectOutputStream obo = new ObjectOutputStream(bo);
			Log.d("-----------message-------", "loaded");
			obo.writeObject(new Integer(mode));
			obo.close();
			bo.close();
			fo.close();
		} catch (Exception ex) {
		}
	}

	public int readMode() throws IOException {
		try {
			if (context == null)
				Log.d("----Context null onread mode OCR", "thoainguyen");
			FileInputStream fi = context.openFileInput(MODE_FILE_NAME);
			BufferedInputStream bi = new BufferedInputStream(fi);
			ObjectInputStream oi = new ObjectInputStream(bi);
			Integer i = null;
			while (bi.available() > 0) {
				i = (Integer) oi.readObject();
				if (i != null) {
					Log.d("---OCR mode:" + i.intValue(), "thoainguyen");
				}
			}
			oi.close();
			bi.close();
			fi.close();
			return i.intValue();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
}
